import {defineStore} from "pinia";
import axios from "axios";

export  const useHitokotoStore = defineStore("hitokoto",{
    state: () => ({
        data:{
            id: null ,
            uuid: "",
            hitokoto: "",
            type: "",
            from: "",
            from_who: null,
            creator: "",
            creator_uid: null,
            reviewer: null,
            commit_from: "",
            created_at: "",
            length: null
        },
        hitokoto:"流淌吧,过往的刀光"
    }),
    getters:{
        SET_Hitokoto: (state ,hitokoto) =>{
            state.hitokoto = hitokoto
        },
        SET_Data: (state ,data) =>{
            state.data = data
        }
    },
    actions: {
        //获取一言接口数据
        GetHitoKoto() {
            return new Promise(async (resolve, reject) => {
                await axios.get('https://v1.hitokoto.cn')
                    .then(({data}) => {
                        resolve(data)
                    })
                    .catch(console.error)
            })
        },
        async CreateHitoKoto() {
            await this.GetHitoKoto().then(res => {
                this.data = res;
                this.hitokoto = res.hitokoto;
            }).catch(() => {
                this.hitokoto = '流通吧,过往的刀光';
            })
        }
    }
});

